package com.zhao.controller;

import com.zhao.dao.SuperviseDao;
import com.zhao.pojo.*;
import com.zhao.service.DrivingLicenseService;
import com.zhao.service.SuperviseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpSession;

@Controller
public class SuperviseController {

    @Autowired
    SuperviseService superviseService;

    @Autowired
    SuperviseDao superviseDao;

    @Autowired
    private DrivingLicenseService drivingLicenseService;

    /////////////////////////////  admin    //////////////////////////////////////

    @ResponseBody
    @RequestMapping("admin-seeOneSuperviseForm.do")
    public String adminSeeOneSuperviseForm(String superviseFormID,HttpSession session){

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        String res = superviseService.adminSeeOneSuperviseForm(admin,superviseFormID,session);
        System.out.println(res);

        return res;
    }

    @RequestMapping("admin-getAllSupervises.do")
    public String adminGetAllSupervises(HttpSession session){

        Admin admin = (Admin)session.getAttribute("admin");

        if(admin == null){
            return "redirect:/adminLogin";
        }

        String res = superviseService.adminGetAllSupervises(admin,session);
        System.out.println(res);
        if(!res.equals("success")){
            return "404";
        }

        return "redirect:/admin-allSupervises";
    }


    /////////////////////////////  校警     //////////////////////////////////////

    @RequestMapping("superviser-ToSupervise.do")
    private String adminGetAllSuperviser(HttpSession session){

        session.removeAttribute("drivingLicenseImpl");
        session.removeAttribute("scanEbick");
        return "redirect:/superviser-supervise";
    }

    @RequestMapping("superviserGetSupervise.do")
    public String superviserGetSupervise(HttpSession session){

        Superviser superviser = (Superviser) session.getAttribute("superviser");
        if(superviser==null){
            return "redirect:/";
        }

        String res = superviseService.superviserGetSupervise(session);

        System.out.println(res);

        return "redirect:/superviser-allSupervises";

    }

    @ResponseBody
    @RequestMapping("superviserUploadSuperviseForm.do")
    public String superviserUploadSuperviseForm(@RequestParam("ebickID") String ebickID,
                                                @RequestParam("superviseType") String superviseType,
                                                @RequestParam("details") String details ,
                                                @RequestParam("operationType") String operationType ,
                                                @RequestParam("image") MultipartFile file ,
                                                HttpSession session){

        SuperviseForm superviseForm = new SuperviseForm();

        Superviser superviser = (Superviser) session.getAttribute("superviser");
        if(superviser == null){
            return "请先登录";
        }

        if(ebickID != null && superviseType != null && file != null && operationType != null && session.getAttribute("superviser") != null){
            superviseForm.setEbickID(ebickID);
            superviseForm.setSuperviseType(superviseType);
            superviseForm.setDetails(details);
            superviseForm.setImage(file.getOriginalFilename());

            if(operationType.equals("提醒纠正")){
                superviseForm.setStatus("待改正");
            }else {
                superviseForm.setStatus("待受理");
            }

            System.out.println(superviseForm);
            String res = superviseService.superviserUploadSuperviseForm(superviseForm,file ,session);
            System.out.println(res);

            if(res.equals("succcess")){
                return "success";
            }else {
                return res;
            }
        }else {
            /////!!!!待处理
            System.out.println("error");
            return "每个字段不能为空";
        }

    }

    //////////////////////辅导员  ///////////////
    //提醒学生改正违规
    @RequestMapping("remindStudent.do")
    public String remindStudent(String superviseID,HttpSession session){

        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor") ;
        if(schoolCounsellor == null){
            System.out.println("请登录");
            return "/";
        }
        String res = superviseService.remindStudent(superviseID,schoolCounsellor);
        System.out.println(res);
        if(!res.equals("success")){
            return "404";
        }


        return "redirect:/getSuperviseMyStudent.do";
    }


    ///查看自己的学生被举报的情况
    @RequestMapping("getSuperviseMyStudent.do")
    public String getSuperviseMyStudent(HttpSession session){

        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor") ;
        if(schoolCounsellor == null){
            System.out.println("请登录");
            return "/";
        }

        String res = superviseService.getSuperviseMyStudent(schoolCounsellor,session);
        System.out.println(res);
        if(!res.equals("success")){
            return "404";
        }

        return "redirect:/newShowSuperviseMyStudent";
    }

    //////////////////////////////  学生  //////////////////////////////////////////////////////

    @RequestMapping("delete.do")
    public String deleteSupervise(String superviseID,HttpSession session){
        System.out.println(superviseID);
        String res = superviseService.deleteSupervise(superviseID,session);
        System.out.println(res);
        if(res.equals("请先登录")){
            return "redirect:/";
        }else if(res.equals("error")){
            return "404";
        }
        Student student = (Student)session.getAttribute("student");
        Superviser superviser = (Superviser) session.getAttribute("superviser");
        if(student!=null){
            return "redirect:/showMySupervise.do";
        }else{
            return "redirect:/superviserGetSupervise.do";
        }

    }

    @RequestMapping("uploadAppeal.do")
    public String uploadAppeal(String appealReason,HttpSession session){

        System.out.println(appealReason);
        String res = superviseService.uploadAppealSupervise(appealReason,session);
        System.out.println(res);
        return "newShowSuperviseMe";
    }

    @RequestMapping("appeal.do")
    public String ToAppeal(String superviseID,HttpSession session){
        System.out.println(superviseID);
        SuperviseForm superviseForm = superviseDao.findByID(superviseID);
        session.setAttribute("appealSupervise",superviseForm);
        return "newStudentAppeal";
    }

    @RequestMapping("uploadCorrect.do")
    public String uploadCorrect(MultipartFile correctImage,HttpSession session){

        System.out.println(correctImage.getOriginalFilename());
        String res = superviseService.uploadCorrectSupervise(correctImage,session);
        System.out.println(res);
        return "newShowSuperviseMe";
    }

    @RequestMapping("correct.do")
    public String ToCorrect(String superviseID,HttpSession session){
        System.out.println(superviseID);
        SuperviseForm superviseForm = superviseDao.findByID(superviseID);
        session.setAttribute("needCorrectSupervise",superviseForm);
        return "newStudentCorrect";
    }

    @RequestMapping("showSuperviseMe.do")
    public String showSuperviseMe(HttpSession session){

        Student student = (Student)session.getAttribute("student") ;
        if(student == null){
            System.out.println("请登录");
            return "/";
        }

        System.out.println("showSuperviseMe.do");
        String res = superviseService.getSuperviseMe(session);
        System.out.println(res);
        return "newShowSuperviseMe";
    }

    @RequestMapping("showMySupervise.do")
    public String showMySupersive(HttpSession session){

        String res = superviseService.getMySupervise(session);

        System.out.println(res);

        return "redirect:/newShowMySupervise";
    }

    @ResponseBody
    @RequestMapping("uploadSuperviseForm.do")
    public String uploadSuperviseForm(@RequestParam("ebickID") String ebickID,
                                      @RequestParam("superviseType") String superviseType,
                                      @RequestParam("details") String details ,
                                      @RequestParam("operationType") String operationType ,
                                      @RequestParam("image") MultipartFile file ,
                                      HttpSession session){

        SuperviseForm superviseForm = new SuperviseForm();

        if(ebickID != null && superviseType != null && file != null && operationType != null && session.getAttribute("student") != null){
            superviseForm.setEbickID(ebickID);
            superviseForm.setSuperviseType(superviseType);
            superviseForm.setDetails(details);
            superviseForm.setImage(file.getOriginalFilename());

            if(operationType.equals("提醒纠正")){
                superviseForm.setStatus("待改正");
            }else {
                superviseForm.setStatus("待受理");
            }

            System.out.println(superviseForm);
            String res = superviseService.uploadSuperviseForm(superviseForm,file ,session);
            System.out.println(res);

            if(res.equals("succcess")){
                return "success";
            }else {
                return res;
            }
        }else {
            /////!!!!待处理
            System.out.println("请填完整举报信息");
            return "请填完整举报信息";
        }
    }
}
